package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.organ
 * 类描述:    内部部门dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class NbbmDao extends SimpleHibernateTemplate<Nbbm, Long> {
    /**
     * 查询内部部门列表
     *
     * @param id       内部部门Id
     * @param mc       内部部门名称
     * @param parentId 上级部门Id
     * @param parentMc 上级部门名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Nbbm> findNbbmList(Long id, String mc, Long parentId, String parentMc, int pageNo, int pageSize) {
        Page<Nbbm> page = new Page<Nbbm>(pageNo, pageSize, 0);
        page.setAutoCount(true);
        page.setOrder("asc");
        page.setOrderBy("pxh");
        List<Criterion> criterions = new ArrayList<Criterion>();

        if (YgService.getCurrentYgCxfw() == 2 || YgService.getCurrentYgCxfw() == 3) {
            criterions.add(Restrictions.eq("id", YgService.getCurrentYgGsId()));
        }

        if (id != null) {
            criterions.add(Restrictions.eq("id", id));
        } else {
            if (StringUtils.isNotEmpty(mc)) {
                criterions.add(Restrictions.or(Restrictions.like("qc", mc, MatchMode.START), Restrictions.like("jc", mc, MatchMode.START)));
            }
        }
        if (parentId != null) {
            criterions.add(Restrictions.eq("sjjg.id", parentId));
        } else {
            if (StringUtils.isNotEmpty(parentMc)) {
                criterions.add(Restrictions.or(Restrictions.like("sjjg.jc", parentMc, MatchMode.START),
                        Restrictions.like("sjjg.qc", parentMc, MatchMode.START)));
            }
        }
        return this.findByCriteria(page, criterions);
    }

    /**
     * 判断属性是否重复
     *
     * @param id            内部部门Id
     * @param propertyValue 内部部门属性值
     * @param propertyName  内部部门属性名
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Nbbm where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Nbbm where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

    /**
     * 查询所有根节点内部部门
     *
     * @return
     */
    public List<Nbbm> findAllRootNbbm() {
        return find("from Nbbm where sjjg.sjjg = null and sfzx = false order by pxh asc, dateCreated desc");
    }

    public List<Nbbm> findGyz(Long gsId) {
        return find("from Nbbm where sfgyz = true and sfzx = false and sjjg.id = ? order by pxh asc, dateCreated desc", gsId);
    }

    /**
     * 获取经营部门
     *
     * @param gsId
     * @return
     */
    public List<Nbbm> findJybm(Long gsId) {
        return find("from Nbbm where sfjyzz = true and sfzx = false and sjjg.id = ? order by pxh asc, dateCreated desc", gsId);
    }

    /**
     * 获取保供业务部门
     * @return
     */
    public List<Nbbm> findBgywbm() {
        return find("from Nbbm where  sfjyzz = true and sfbgyw = true and sfzx = false order by pxh asc, dateCreated desc");
    }

    /**
     * 获取部门科目代码
     *
     * @param gsId
     * @return
     */
    public List<Map> findKmdm(Long gsId) {
        String sql = "select kmdm as \"kmdm\",qc as \"bmqc\" from t_sys_gsjg " +
                " where (type='nbbm' or type='nbgs') and deleted=0 and length(rtrim(ltrim(kmdm)))!=0 and sjjg_id=? ";
        List<Map> result = createSQLQuery(sql, gsId).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        return result;
    }
}
